/**
 * Description: 创建消息队列并发送消息
 * File: send.c |Time: 2021-04-07 15:12
 */

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>

#define MSG_SIZE 1024
#define MSG_TYPE 1

struct msgbuf {
    long msg_type;
    char msg_text[MSG_SIZE];
};

int main(int argc, char *argv[])
{
    key_t q_key = ftok("/home/b", 'a');
    
    if (-1 == q_key) {
        perror("ftok");
        return -1;
    }

    int msg_id = msgget(q_key, IPC_CREAT|0666);

    if (msg_id == -1) {
        perror("msgget");
        return -1;
    }

    struct msgbuf msg_node;

    msg_node.msg_type = MSG_TYPE;
    printf("please input msg: ");
    fgets(msg_node.msg_text, MSG_SIZE, stdin);

    int send_res = msgsnd(msg_id, &msg_node, MSG_SIZE, 0);

    if (send_res == -1) {
        perror("msgsnd");
        return -1;
    }

    return 0;
}

